package com.hanthink.system.service.impl.report;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import com.hanthink.system.domain.pc.EmTask;
import com.hanthink.system.domain.report.*;
import com.hanthink.system.mapper.EmTaskMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.util.StringUtil;
import com.hanthink.system.mapper.report.ReportMapper;
import com.hanthink.system.service.report.IReportInfoService;

@Service
public class ReportInfoServiceImpl implements IReportInfoService {

	@Autowired
	private ReportMapper reportMapper;

	@Autowired
	private EmTaskMapper emTaskMapper;

	@Override
	public List<ReportInfo> queryReportInfo(ReportInfo reportInfo) {
		// 查询部门信息
		return reportMapper.queryReportInfo(reportInfo);
	}

	@Override
	public List<SiteInfo> querySiteInfo(SiteInfo info) {

		return reportMapper.querySiteInfo(info);
	}

	// 查询未完成任务点检表格数据
	@Override
	public List<ReportInfo> querytableReportInfo(ReportInfo reportInfo) {
		List<ReportInfo> departList = reportMapper.queryDepartInfo(reportInfo);
		List<ReportInfo> resultDepartList = new ArrayList<ReportInfo>();
		if (departList.size() > 0) {

			// 过滤部门
			if (reportInfo.getDepartment() != null && !"".equals(reportInfo.getDepartment())) {
				Iterator<ReportInfo> iterator = departList.iterator();
				while (iterator.hasNext()) {
					ReportInfo info = iterator.next();
					if (!info.getDepartment().equals(reportInfo.getDepartment())) {
						iterator.remove();
					}

				}
			}
			List<ReportInfo> infoList = reportMapper.queryReportInfo(reportInfo);
			for (ReportInfo parentInfo : departList) {
				for (ReportInfo childInfo : infoList) {

					// 如果部门相同，则组合拼接数据
					if (parentInfo.getDepartment().equals(childInfo.getDepartment())) {

						if (childInfo.getTaskType().equals("日任务")) {
							parentInfo.setDayTaskNum(childInfo.getTaskNum());
						}
						else if (childInfo.getTaskType().equals("周任务")) {
							parentInfo.setWeekTaskNum(childInfo.getTaskNum());
						}
						else if (childInfo.getTaskType().equals("工艺点检")) {
							parentInfo.setTechTaskNum(childInfo.getTaskNum());
						}
						else
						{
							parentInfo.setMonthTaskNum(childInfo.getTaskNum());
						}

					}

				}
			}
//			for (ReportInfo parentInfo : departList) {
//				for (ReportInfo childInfo : infoList) {
//					// 如果部门相同，则组合拼接数据
//					if (parentInfo.getDepartment().equals(childInfo.getDepartment())) {
//						switch (childInfo.getTaskType()) {
//						case "日任务":
//							parentInfo.setDayTaskNum(childInfo.getTaskNum());
//							break;
//						case "周任务":
//							parentInfo.setWeekTaskNum(childInfo.getTaskNum());
//							break;
//						case "月任务":
//							try {
//								if (reportInfo.getType() == 2) {
//									parentInfo.setMonthTaskNum(childInfo.getTaskNum());
//								}
//								else
//								{
//									EmTask emTask = new EmTask();
//									emTask.setSiteKey(String.valueOf(reportInfo.getSiteKey()));
//									SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//									Date date1 = sdf.parse(reportInfo.getStartTime());
//									Date date2 = sdf.parse(reportInfo.getEndTime());
//									emTask.setStartTime(date1);
//									emTask.setEndTime(date2);
//									emTask.setDepartment(childInfo.getDepartment());
//									Integer s =  emTaskMapper.selectEmTaskListSUMS(emTask);
//									s=  (s== null )? 0 : s;
//									parentInfo.setMonthTaskNum(s);
//								}
//							} catch (ParseException e) {
//								throw new RuntimeException(e);
//							}
//							break;
//						case "工艺点检":
//                            try {
//								if (reportInfo.getType() == 2) {
//									parentInfo.setTechTaskNum(childInfo.getTaskNum());
//								}
//								else
//								{
//									EmTask emTask = new EmTask();
//									emTask.setSiteKey(String.valueOf(reportInfo.getSiteKey()));
//									SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//									Date date1 = sdf.parse(reportInfo.getStartTime());
//									Date date2 = sdf.parse(reportInfo.getEndTime());
//									emTask.setStartTime(date1);
//									emTask.setEndTime(date2);
//									emTask.setDepartment(childInfo.getDepartment());
//									Integer s =  emTaskMapper.selectEmTaskListSUM(emTask);
//									s=  (s== null )? 0 : s;
//									parentInfo.setTechTaskNum(s);
//								}
//                            } catch (ParseException e) {
//                                throw new RuntimeException(e);
//                            }
//							break;
//						default:
//							break;
//						}
//					}
//
//
//				}
//
////				resultDepartList.add(parentInfo);
//			}

			if (departList.size() > 0) {
				for (ReportInfo parentInfo : departList) {
					if (parentInfo.getDayTaskNum() > 0 || parentInfo.getWeekTaskNum() > 0
							|| parentInfo.getMonthTaskNum() > 0 || parentInfo.getTechTaskNum() > 0) {
						resultDepartList.add(parentInfo);
					}
				}
			}
		}
		return resultDepartList;
	}

	@Override
	public List<ReportInfo> queryRepairReportInfo(ReportInfo reportInfo) {

		return reportMapper.queryRepairReportInfo(reportInfo);
	}

	@Override
	public List<ReportInfo> queryList(ReportInfo reportInfo) {
		if (StringUtil.isNotEmpty(reportInfo.getTaskType()) && reportInfo.getTaskType().equals("3")
				&& reportInfo.getType() == 1) {
			return reportMapper.queryTechNoFinishList(reportInfo);
		} else {
			return reportMapper.queryList(reportInfo);
		}

	}

	@Override
	public List<ReportInfo> queryListPlus(ReportInfo reportInfo) {
			return reportMapper.queryListPlus(reportInfo);
	}


	@Override
	public List<ReportInfo> queryDepartInfo(ReportInfo reportInfo) {
		return reportMapper.queryDepartInfo(reportInfo);
	}

	@Override
	public List<ReportInfo> queryMyTaskList(ReportInfo reportInfo) {
		return reportMapper.queryMyTaskList(reportInfo);
	}

	@Override
	public List<RequestAndRepairRecord> queryRequestAndRepair(RequestAndRepairRecord requestAndRepairRecord) {

		return reportMapper.queryRequestAndRepair(requestAndRepairRecord);
	}

	@Override
	public List<RequestAndRepairRecordByTime> queryRequestAndRepairbyTime(RequestAndRepairRecord requestAndRepairRecord) {

		return reportMapper.queryRequestAndRepairbyTime(requestAndRepairRecord);
	}

	@Override
	public List<RepairTimeRecord> queryRepairTimeRecord(RepairTimeRecord repairTimeRecord) {
		return reportMapper.queryRepairTimeRecord(repairTimeRecord);
	}

}
